package cn.xz.gobang.util

import cn.xz.gobang.bean.*
import cn.xz.gobang.net.OkHttpUtil
import com.google.gson.Gson
import okhttp3.FormBody
import okhttp3.RequestBody
import java.util.*

/**
 * 获取数据工具*/
object GetDataUtils {


    //已经下棋的合集
    private var mData: RoomSingle? = null

    fun getData(): RoomSingle{
        if (mData == null){
            mData = RoomSingle()
        }
        return mData!!
    }

    fun setData(m: RoomSingle){
        mData = m
    }


    /**
     * 获取缓存中的用户数据*/
    fun getUserCache(): UserData?{
        var mD: UserData? = null
        val mS = SessionUtils.getString(SessionUtils.USER_DATA,"")
        if (mS != null && mS.isNotEmpty()){
            mD = Gson().fromJson<UserData>(mS, UserData::class.java)
        }
        return mD
    }

    /**获取用户信息*/
    interface OnReadDataListener {
        fun success(bean: UserData)
        fun error()
    }
    fun onUserData(txtName: String,onLister: OnReadDataListener){
        val requestBody: RequestBody = FormBody.Builder()
            .add("txtName",txtName)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.GET_ALL_USER,requestBody,object :
            OkHttpUtil.RequestSuccess<UserData> {
            override fun success(bean: UserData?) {
                if (bean != null){
                    onLister.success(bean)
                }
            }
            override fun error() {
                onLister.error()
            }
        }, UserData::class.java)
    }

    /**修改或者添加文件*/
    interface OnWriteDataListener {
        fun success()
    }

    fun onWriteData(txtName: String,content: String,onLister: OnWriteDataListener){
        val newBody: RequestBody = FormBody.Builder()
            .add("txtName", txtName)
            .add("txtContent", content)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.UpData_User_Data,newBody,object :
            OkHttpUtil.RequestSuccess<GoBangUser> {
            override fun success(bean: GoBangUser?) {
                //此时将用户信息存到本地
                onLister.success()
            }
            override fun error() {
            }
        }, GoBangUser::class.java)
    }

    /**获取单人匹配房间列表*/
    interface OnSingleDataListener {
        fun success(bean: Single)
        fun error()
    }

    fun onSingleData(onLister: OnSingleDataListener){
        val requestBody: RequestBody = FormBody.Builder()
            .add("txtName", CommonLib.SINGLE)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.GET_ALL_USER,requestBody,object :
            OkHttpUtil.RequestSuccess<Single> {
            override fun success(bean: Single?) {
                if (bean != null && bean.data.isNotEmpty()){
                    onLister.success(bean)
                }else{
                    //无等待房间，创建一个房间
                    onLister.error()
                }
            }
            override fun error() {
            }
        }, Single::class.java)
    }

    /**修改单人房列表数据*/
    fun upDataSingleData(mHomeBean: Single){
        onWriteData(CommonLib.SINGLE,JsonUtil.serializeAsString(mHomeBean),object :OnWriteDataListener{
            override fun success() {
            }

        })
    }

    /**查询单人房数据*/
    interface OnSingleRoomListener {
        fun success(bean: RoomSingle)
    }

    fun onSingleRoomData(txtName: String,onLister: OnSingleRoomListener){
        val requestBody: RequestBody = FormBody.Builder()
            .add("txtName", txtName)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.GET_ALL_USER,requestBody,object :
            OkHttpUtil.RequestSuccess<RoomSingle> {
            override fun success(bean: RoomSingle?) {
                if (bean != null){
                    //开始游戏
                    onLister.success(bean)

                }
            }
            override fun error() {
            }
        }, RoomSingle::class.java)
    }

    /**删除房间*/
    fun deleteRoom(txtName: String){
        val newBody: RequestBody = FormBody.Builder()
            .add("txtName", txtName)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.DELETE_TXT,newBody,object :
            OkHttpUtil.RequestSuccess<GoBangUser> {
            override fun success(bean: GoBangUser?) {
            }
            override fun error() {
            }
        }, GoBangUser::class.java)
    }


    /**加减积分*/
    fun setUserPart(add: Boolean, n: Int) {
        //先查询我的数据
        var mJ = SessionUtils.getInt(SessionUtils.USER_GET_NUM, 0)
        if (add) {
            //加
            mJ += n
        } else {
            if (mJ - n > -1) {
                mJ -= n
            } else {
                mJ = 0
            }
        }
        SessionUtils.commitInt(SessionUtils.USER_GET_NUM, mJ)
    }

    /**加对局数*/
    fun setUserPk(){
        //先查询我的数据
        onUserData(CommonLib.getUserID,object :OnReadDataListener{
            override fun success(bean: UserData) {
                //加
                bean.gameNumber = bean.gameNumber + 1
                //更新
                onWriteData(CommonLib.getUserID,JsonUtil.serializeAsString(bean),object :OnWriteDataListener{
                    override fun success() {
                    }

                })
            }
            override fun error() {
            }
        })
    }

    /**获取多人匹配房间列表*/
    interface OnDoubleDataListener {
        fun success(bean: RoomDouble)
        fun error()
    }
    fun onDoubleData(onLister: OnDoubleDataListener){
        val requestBody: RequestBody = FormBody.Builder()
            .add("txtName", CommonLib.DOUBLE)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.GET_ALL_USER,requestBody,object :
            OkHttpUtil.RequestSuccess<RoomDouble> {
            override fun success(bean: RoomDouble?) {
                if (bean != null && bean.data.isNotEmpty()){
                    onLister.success(bean)
                }else{
                    //无等待房间，创建一个房间
                    onLister.error()
                }
            }
            override fun error() {
            }
        }, RoomDouble::class.java)
    }

    /**修改多人房列表数据*/
    fun upDataDoubleData(mHomeBean: RoomDouble){
        onWriteData(CommonLib.DOUBLE,JsonUtil.serializeAsString(mHomeBean),object :OnWriteDataListener{
            override fun success() {
            }

        })
    }

    /**
     * 查询多人房间详情信息*/
    interface OnDoubleRoomListener {
        fun success(bean: DoubleRoomData)
        fun error()
    }

    fun onDoubleRoomData(txtName: String,onLister: OnDoubleRoomListener){
        val requestBody: RequestBody = FormBody.Builder()
            .add("txtName", txtName)
            .build()
        OkHttpUtil.toPost(CommonLib.getMUrl + CommonLib.GET_ALL_USER,requestBody,object :
            OkHttpUtil.RequestSuccess<DoubleRoomData> {
            override fun success(bean: DoubleRoomData?) {
                if (bean != null){
                    //开始游戏
                    onLister.success(bean)

                }
            }
            override fun error() {
                onLister.error()
            }
        }, DoubleRoomData::class.java)
    }
}